package com.zhou.listener.myapplication;

import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;

import com.gyf.barlibrary.ImmersionBar;

public class MainActivity extends AppCompatActivity {

    private CollapsingToolbarLayoutState state;
    private TextView subTitle;
    private String title="中级经济师基础课程是";
    private enum CollapsingToolbarLayoutState {
        EXPANDED,
        COLLAPSED,
        INTERNEDIATE
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final CollapsingToolbarLayout toolbar = findViewById(R.id.toolBarLayout);
        final Toolbar mToolBar = findViewById(R.id.toolbar);
        subTitle = findViewById(R.id.subTitle);
        subTitle.setText(title);
//        toolbar.setTitleTextAppearance(this,R.style.title_style1);

        AppBarLayout  app_bar=(AppBarLayout)findViewById(R.id.appBar);
        app_bar.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {

                if (verticalOffset == 0) {
                    if (state != CollapsingToolbarLayoutState.EXPANDED) {//完全拉下
                        state = CollapsingToolbarLayoutState.EXPANDED;//修改状态标记为展开
                        toolbar.setTitle("");//设置title为EXPANDED
                        subTitle.setVisibility(View.VISIBLE);
                    }
                } else if (Math.abs(verticalOffset) >= appBarLayout.getTotalScrollRange()-mToolBar.getMeasuredHeight()/3) {//完全上去
                    if (state != CollapsingToolbarLayoutState.COLLAPSED) {
                        toolbar.setTitle(title);//设置title不显示
                        subTitle.setVisibility(View.GONE);
//                        playButton.setVisibility(View.VISIBLE);//隐藏播放按钮
                        state = CollapsingToolbarLayoutState.COLLAPSED;//修改状态标记为折叠
                    }
                } else {
                    if (state != CollapsingToolbarLayoutState.INTERNEDIATE) {//上去一点
//                        if(state == CollapsingToolbarLayoutState.COLLAPSED){
//                            playButton.setVisibility(View.GONE);//由折叠变为中间状态时隐藏播放按钮
//                        }
                        toolbar.setTitle("");//设置title为INTERNEDIATE
                        subTitle.setVisibility(View.VISIBLE);
                        state = CollapsingToolbarLayoutState.INTERNEDIATE;//修改状态标记为中间
                    }
                }
            }
        });
        ImmersionBar.with(this)
                .statusBarView(R.id.top_view)
                .navigationBarColor(R.color.colorPrimary)
                .fullScreen(true)
                .addTag("PicAndColor")
                .init();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImmersionBar.with(this).destroy(); //必须调用该方法，防止内存泄漏
    }
}
